<template>
	<view class="assemblyOrder-container">
		<view class="dome pt20 pb50 pl20 pr20 ">
			<view class="flex mb10">
				<view class="w300">
					生产单号:
				</view>
				<view class="w480" style="word-break: break-word;">
					{{ info.semiProductionCode || '' }}
				</view>
			</view>

			<view class="flex mb10">
				<view class="w300">
					制单日期:
				</view>
				<view class="w480" style="word-break: break-word;">
					{{ info.createTime || '' }}
				</view>
			</view>

			<view class="flex mb10">
				<view class="w300">
					交货日期:
				</view>
				<view class="w480" style="word-break: break-word;">
					{{ info.deliveryDate || '' }}
				</view>
			</view>

			<view class="flex mb10">
				<view class="w300">
					半成品名称:
				</view>
				<view class="w480" style="word-break: break-word;">
					{{ info.specsDetail || '' }}
				</view>
			</view>

			<view class="flex mb10">
				<view class="w300">
					生产数量:
				</view>
				<view class="w480" style="word-break: break-word;">
					{{ info.produceNumber || '' }}
				</view>
			</view>

			<!-- 开始生产 -->
			<template v-if="type === '0'">
				<view class="flex mb10">
					<view class="w300">
						上道工序名称:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.startProduceDetailVo.lastWorkName || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						外协厂:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.startProduceDetailVo.lastFactoryName || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						纸张采购:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.startProduceDetailVo.paperPurchaseNumber || 0 }}{{ info.unit || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						上道工序完成数量:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.startProduceDetailVo.lastWorkCompleteNum || 0 }}{{ info.unit || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						本道工序名称:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.workName || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						外协厂:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.factoryName || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						纸张信息:
					</view>
					<view v-if="info.weight" class="w480" style="word-break: break-word;">
						{{ info.weight }}g*{{ info.width }}mm*{{ info.lengths }}mm
					</view>
				</view>

				<view class=" mb10 aCenter">
					<view class="w300">
						本道工序接收数量:
					</view>
					<view style="word-break: break-word;">
						<u--input placeholder="请输入接收数量" border="surround" type="number" v-model="number"></u--input>
					</view>
				</view>
			</template>
			<!-- 工序录入 -->
			<template v-if="type === '1'">

				<view class="flex mb10">
					<view class="w300">
						纸张采购:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.paperPurchaseNumber || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						本道工序名称:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.workName || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						本道工序完成数量:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.currentCompleteNum || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						外协厂:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.factoryName || '' }}
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						纸张信息:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.weight }}g*{{ info.width }}mm*{{ info.lengths }}mm
					</view>
				</view>

				<view class="flex mb10">
					<view class="w300">
						计价方式:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.unit || '' }}
					</view>
				</view>


				<view class="aCenter mb10">
					<view class="w300">
						本次完成量:
					</view>
					<view class="w480" style="word-break: break-word;">
						<u--input placeholder="请输入本次完成量" border="surround" type="number"
							v-model="form.number"></u--input>
					</view>
				</view>

				<view class="aCenter mb10">
					<view class="w300">
						每张单价:
					</view>
					<view class="w480" style="word-break: break-word;">
						<u--input :disabled="disabled" placeholder="请输入每张单价" border="surround" type="digit"
							v-model="form.price"></u--input>
					</view>
				</view>

				<view class="aCenter mb10">
					<view class="w300">
						额外支出:
					</view>
					<view class="w480" style="word-break: break-word;">
						<u--input :disabled="disabled" placeholder="请输入额外支出" border="surround" type="digit"
							v-model="form.extraFees"></u--input>
					</view>
				</view>

				<view class="aCenter mb10">
					<view class="w300">
						总金额:
					</view>
					<view class="w480" style="word-break: break-word;">
						￥{{ TotalAmount}}
					</view>

				</view>

				<view class="aCenter mb10">
					<view class="w300">
						工序完成状态:
					</view>
					<view class="w480" style="word-break: break-word;">
						<view class="" style="height: 50rpx;">
							<u-switch v-model="overFlag"></u-switch>
						</view>
					</view>
				</view>
			</template>
			<!-- 配件入库 -->
			<template v-if="type === '3'">
				<view class="flex mb10">
					<view class="w300">
						入库仓库名称:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.warehouseName || '' }}
					</view>
				</view>
				<view class="flex mb10">
					<view class="w300">
						总生产数量:
					</view>
					<view class="w480" style="word-break: break-word;">
						{{ info.inNumber || '' }}
					</view>
				</view>
				<view class="flex mb10">
					<view class="w300">
						此工序完结:
					</view>
					<view class="w480" style="word-break: break-word;">
						<view class="" style="height: 50rpx;">
							<u-switch v-model="overFlag"></u-switch>
						</view>
					</view>
				</view>

				<view class="flex mt20 mb10">
					<view class="list_cen" style="flex: 1;">
						部件名称
					</view>
					<view class="list_cen" style="flex: 2;">
						入库数量
					</view>
				</view>


				<view class="flex mb10" v-for="(item, index) in childrenVoList" :key="index">
					<view class="list_cen" style="flex: 1;">
						{{ item.componentName }}
					</view>
					<view class="list_cen jCenter" style="flex: 2;">
						<view style="width: 300rpx;">
							<u--input placeholder="请输入本次完成量" border="surround" type="number"
								v-model="item.inventoryQuantity"></u--input>
						</view>
					</view>
				</view>

			</template>

			<view class="" style="margin-top: 140rpx;">
				<view class="text" v-if="type === '0'">
					注意:请仔细核对接受数量并如实填写，如生产中入库数量与上道工序完成数量过大，由当前供应商承担误差责任
				</view>
				<view class="text" v-if="type === '1'">
					一但保存无法修改或删除，请仔细核对信息
				</view>

				<view class="jCenter" style="margin-top: 40rpx;">
					<view class="" style="width: 200rpx;">
						<u-button @click="handleAssemblyInformation" size="small" plain type="primary"
							text="确定"></u-button>
					</view>
				</view>
			</view>
		</view>


		<!-- <view class="footer">
			<view class="flex" style="padding: 0 50rpx;">
				<u-button @click="handleAssemblyInformation" type="success" text="录入组装信息"></u-button>
				<view class="ml20 mr20"></view>
				<u-button @click="handlePackaging" type="error" text="录入包装信息"></u-button>
			</view> -->

		<!-- 
			<view class="text">
				该订单还未进入到您所属的工序中，暂时无法进行操作
			</view> -->
		<!-- </view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',
				ids: '',
				info: {},
				number: '',
				form: {
					extraFees: '',
					price: '',
					number: '',
				},
				TotalAmount: 0,
				overFlag: false,
				disabled: false,
				childrenVoList: [],
			}
		},
		onLoad(e) {
			this.ids = e.id
			this.handleInfo()
		},
		watch: {
			form: {
				handler(newName, oldName) {
					//执行代码
					console.log(newName, 123)
					if (newName.price && newName.extraFees) {
						this.TotalAmount = Number(newName.price) + Number(newName.extraFees)
					} else if (newName.price) {
						this.TotalAmount = Number(newName.price)
					} else {
						this.TotalAmount = 0
					}
				},
				immediate: true,
				deep: true
			}
		},
		methods: {
			// 获取相信信息
			async handleInfo() {
				try {
					const {
						data
					} = await this.$api.getDetailsOfSemiFinishedProductsApi(this.ids)

					if (data) {
						this.info = data

						this.type = data.completeStatus

						if (data.completeStatus === '1') {
							if (data.currentCompleteNum) {
								this.disabled = true

								this.form.price = data.price
								this.form.extraFees = data.extraFees

							}
						}

						if (data.lastProduceStatus && data.lastProduceStatus === '2') {

							this.type = '3'

							this.getAccessoriesInfo()
						}
					}



					/**  completeStatus == 0 开始生产 */
					/**  completeStatus == 1 工序录入 */

				} catch (error) {
					//TODO handle the exception
					console.log(error, '报错')
				}
			},

			// 获取配件入库详情
			async getAccessoriesInfo() {
				try {
					const {
						data
					} = await this.$api.detailsOfAccessoryStorage(this.ids)

					this.info = data

					this.childrenVoList = data.childrenVoList.map((item) => {
						return {
							...item,
							inventoryQuantity: '',
						}
					})
				} catch (error) {
					//TODO handle the exception
				}
			},



			/** 跳转录入组装信息 */
			async handleAssemblyInformation() {


				try {
					const {
						data
					} = await this.$api.getCheckPermissions({
						orderOperation: '',
						processNumber: this.info.supplyId,
						semiFlag: 1
					})
					
					
					if(data === '1') {
						if (this.type === '0') {
							if (!this.number) return this.$msg.error('请输入接收数量')
							this.handleStartProduction()
						}
						
						if (this.type === '1') {
							if (!this.form.number) return this.$msg.error('请输入本次完成量')
							if (!this.form.price) return this.$msg.error('请输入每张单价')
						
							this.handleProcess()
						}
						
						if (this.type === '3') {
							if (this.childrenVoList.length > 0) {
								for (let i = 0; i < this.childrenVoList.length; i++) {
									const row = this.childrenVoList[i];
									if (!row.inventoryQuantity) {
										this.$msg.error(`${row.componentName}的入库数量不能为空`);
										return;
									}
								}
							}
						
						
							this.handleWarehousing()
						}
					}else {
						this.$msg.error('您没有该权限')
					}
					
				} catch (error) {
					//TODO handle the exception
				}


				
			},

			// 配件入库
			async handleWarehousing() {
				try {
					await this.$api.saveSemiProduceInApi({
						semiId: this.info.semiId,
						id: this.ids,
						overFlag: this.overFlag ? 1 : 0,
						warehouseType: this.info.warehouseType,
						dataList: this.childrenVoList.map((item) => {
							return {
								componentId: item.componentId,
								number: item.inventoryQuantity,
							}
						})
					})
					this.$msg.success('入库成功')
					if (this.overFlag) {
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					} else {
						this.getAccessoriesInfo()
					}

				} catch (error) {
					//TODO handle the exception
				}
			},

			// 工序
			async handleProcess() {
				try {
					await this.$api.startInputtingSemiFinishedProductApi({
						...this.form,
						overFlag: this.overFlag ? 1 : 0,
						id: this.info.id,
					})
					this.$msg.success('操作成功')
					this.form = {
						extraFees: '',
						price: '',
						number: '',
					}

					if (this.overFlag) {
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					} else {
						this.handleInfo()
					}
				} catch (error) {
					//TODO handle the exception
				}
			},

			// 开始生产
			async handleStartProduction() {
				try {
					await this.$api.saveSemiProduceAcceptApi({
						number: this.number,
						id: this.info.id,
						supplyId: this.info.startProduceDetailVo.supplyId
					})

					this.$msg.success('操作成功')
					this.number = ''

					this.handleInfo()
				} catch (error) {
					//TODO handle the exception
				}
			},
		},
	}
</script>

<style scoped lang="scss">
	.assemblyOrder-container {
		width: 100%;
		min-height: 100vh;
		background-color: #F5F5F5;

		.dome {
			background-color: #FFFFFF;
			border-radius: 8rpx;
			margin: 10rpx auto;
			width: 710rpx;
			border: 1rpx solid #C8C8C8;
		}
	}

	.text {
		width: 100%;
		text-align: center;
		color: #F76C5F;
		font-size: 26rpx;
	}

	.w300 {
		width: 300rpx;
		font-weight: bold;
		font-size: 30rpx;
	}

	.mb10 {
		margin-bottom: 20rpx !important;
	}

	.list_cen {
		text-align: center;
		font-weight: bold;
		font-size: 30rpx;
	}
</style>